<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * This is a sample module for PyroCMS
 *
 * @author 		Jerel Unruh - PyroCMS Dev Team
 * @website		http://unruhdesigns.com
 * @package 	PyroCMS
 * @subpackage 	Sample Module
 */
class Admin extends Admin_Controller
{
	protected $section = 'commercial_objectives';
	protected $_ci;
	public function __construct()
	{
		parent::__construct();
		$this->_ci = &get_instance();
		// Load all the required classes
		$this->load->library('form_validation');
		$this->load->model('escort_commercial_objectives_m');
		$this->load->model('groups_m');
		$this->load->model('users_m');
		$this->load->model('escort_user_roles_n_benefits_m');
		
		$this->lang->load('commercial_objective');
		// Set the validation rules
		$this->validation_rules = array(
			array(
				'field' => 'commercial_id',
				'label' => 'lang:escort_commercial_objective_commercial_label',
				'rules' => 'trim|max_length[255]|required'
			),
			array(
				'field'	=> 'rate_from',
				'label'	=> 'lang:escort_commercial_objective_rate_from_label'
			),
			array(
				'field' => 'rate_to',
				'label' => 'lang:escort_commercial_objective_rate_to_label',
				'rules' => 'trim'
			),
			array(
				'field' => 'actual_registered',
				'label' => 'lang:escort_commercial_objective_actual_label',
				'rules' => 'trim|required|is_natural'
			),
			array(
				'field' => 'price_per_register',
				'label' => 'lang:escort_commercial_objective_price_label',
				'rules' => 'trim|is_natural|required'
			),
			array(
				'field' => 'active',
				'label' => 'lang:escort_commercial_objective_active_label',
				'rules' => 'trim'
			)
		);

		$this->data->rateFromList = array();
		$this->data->rateToList = array();
		$this->data->priceList = array();
		for ($i = 0; $i < 1000; $i++){
			$this->data->rateFromList[$i] = $i;
			$this->data->rateToList[$i] = $i;
			$this->data->priceList[$i] = $i;
		}
		$commercialGroupId = $this->groups_m->get_by('name', USER_COMMERCIAL)->id;
		$commercials = $this->users_m->get_many_by('group_id', $commercialGroupId);
		$this->data->commercialList = array();
		if($commercials){
			foreach ($commercials as $commercial){
				$user = $this->escort_user_roles_n_benefits_m->get_by('username', $commercial->username);
				if($user){
					$this->data->commercialList[$user->id] = $user->username;
				}
			}
		}

		$discountTypes = $this->_ci->discount_type_entity->getList();
		if($discountTypes){
			foreach ( $discountTypes as $discountType){
				$this->data->discountTypeList[$discountType->id] = $discountType->name;
			}
		}

		$this->template->set_partial('shortcuts', 'admin/partials/shortcuts')
		->append_metadata(js('escort.commercial.js', 'be_commercial_objective'))
		->append_metadata(css('escort.commercial.css', 'be_commercial_objective'));
	}

	/**
	 * List all items
	 */
	public function index()
	{
		// Create pagination links
		$total_rows = $this->_ci->commercial_objective_entity->count();
		$pagination = create_pagination('admin/be_commercial_objective/index', $total_rows);
		// Using this data, get the relevant results
		if($this->_ci->ion_auth->get_user()->username == USER_ADMIN){
			$commercials = $this->_ci->commercial_objective_entity->getList('',$pagination['limit']);
		}else{
			$commercials = $this->_ci->commercial_objective_entity->getList($this->_ci->ion_auth->get_user()->username, $pagination['limit']);
		}
		//do we need to unset the layout because the request is ajax?
		$this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';

		$this->template
		->title($this->module_details['name'])
		->append_metadata(js('admin/filter.js'))
		->set('pagination', $pagination)
		->set('commercials', $commercials);

		$this->input->is_ajax_request() ? $this->template->build('admin/tables/posts', $this->data) : $this->template->build('admin/index', $this->data);
			
	}
}
